/*
 ------------------------------------------------------------------------------
 Copyright (C) 2013 Eternal Games.

 This file is part of the EternalTech source code.

 The EternalTech source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The EternalTech source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the EternalTech source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


//
// cm_leaf.cpp - ...
//


#include "cm_local.h"


/*
 ==================
 CM_DecompressPVS
 ==================
*/
static byte *CM_DecompressPVS (const byte *in){

	static byte pvs[BSP_MAX_LEAFS/8];
	byte		*out;
	int			c, row;

	row = (cm.numLeafs + 7) >> 3;	
	out = pvs;

	if (!in){
		// No pvs info, so make all visible
		while (row){
			*out++ = 0xFF;
			row--;
		}

		return pvs;		
	}

	do {
		if (*in){
			*out++ = *in++;
			continue;
		}

		c = in[1];
		in += 2;
		while (c){
			*out++ = 0;
			c--;
		}
	} while (out - pvs < row);

	return pvs;
}

/*
 ==================
 CM_LeafPVS
 ==================
*/
byte *CM_LeafPVS (clipLeaf_t *leaf){

	if (!cm.loaded)
		Com_Error(false, "CM_LeafPVS: map not loaded");

	if (cm_skipPVS->integerValue || !cm.pvs)
		return cm.noPVS;

	return CM_DecompressPVS(leaf->compressedPVS);
}

/*
 ==================

 ==================
*/
bool CM_PointInLeaf (const glqVec3 &point, int *nodeNum){

	clipLeaf_t	*leaf;
	int			num = 0;

	if (!cm.loaded)
		Com_Error(false, "CM_PointInLeaf: map not loaded");

	cm_stats.leafPoints++;

	while (1){
		// If < 0, we are in a leaf node
		if (num < 0){
			leaf = &cm.leafs[-1 - num];
			break;
		}

		// TODO!!!
	}

	return true;
}